//
//  AgentsCell.m
//  RENTVISITOR
//
//  Created by wonder on 2017/9/6.
//  Copyright © 2017年 UNN. All rights reserved.
//

#import "AgentsCell.h"
#import "Masonry.h"
#import "RentVieitor.h"
#import "Starview.h"
#import "AgentModel.h"
#import "UIImageView+WebCache.h"

@implementation AgentsCell {
    
    CGFloat scal;
    UIImageView *hpView;
    UILabel *nameLabel;
    UILabel *numLabel;
    UILabel *evaluateLabel;
    UIImageView *interImgView;
    UIImageView *estateView;
    UILabel *belongLabel;
    UILabel *beTextLabel;
    UILabel *knowLabel;
    UILabel *tagLabel;
    UILabel *tagLabel2;
    UILabel *tagLabel3;
    Starview *starView;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        scal = KWidth/375;
        if (scal > 1) {
            scal = 1;
        }
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self creatview];
    }
    return self;
}

- (void)creatview {
    
    UIView *headView = [[UIView alloc] init];
    //    headView.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:headView];

    
    hpView = [[UIImageView alloc] init];
    hpView.image = [UIImage imageNamed:@"头像"];
    [headView addSubview:hpView];
    
    nameLabel = [[UILabel alloc] init];
    nameLabel.font = [UIFont systemFontOfSize:17];
    nameLabel.textColor = [Utils colorConvertFromString:@"#101010"];
    nameLabel.text = @"吴彦祖";
    [headView addSubview:nameLabel];
    
    numLabel = [[UILabel alloc] init];
    numLabel.font = [UIFont systemFontOfSize:12];
    numLabel.textColor = [Utils colorConvertFromString:@"#333333"];
    numLabel.text = @"在租房源18套";
    [headView addSubview:numLabel];
    
    
    evaluateLabel = [[UILabel alloc] init];
    evaluateLabel.font = [UIFont systemFontOfSize:12];
    evaluateLabel.textColor = [Utils colorConvertFromString:@"#333333"];
    evaluateLabel.text = @"专业能力5分 响应速度5分 服务态度5分";
    [headView addSubview:evaluateLabel];
    
    interImgView = [[UIImageView alloc] init];
    interImgView.image = [UIImage imageNamed:@"中介"];
    [headView addSubview:interImgView];
    
    estateView = [[UIImageView alloc] init];
    estateView.image = [UIImage imageNamed:@"小区"];
    [headView addSubview:estateView];
    
    belongLabel = [[UILabel alloc] init];
    belongLabel.text = @"所属中介";
    belongLabel.font = [UIFont systemFontOfSize:10];
    belongLabel.textColor = [Utils colorConvertFromString:@"#333333"];
    [headView addSubview:belongLabel];
    
    beTextLabel = [[UILabel alloc] init];
    beTextLabel.text = @"北京华飞房屋租赁有限公司";
    beTextLabel.font = [UIFont systemFontOfSize:10];
    beTextLabel.textColor = [Utils colorConvertFromString:@"#333333"];
    [headView addSubview:beTextLabel];
    
    knowLabel = [[UILabel alloc] init];
    knowLabel.text = @"熟悉小区";
    knowLabel.font = [UIFont systemFontOfSize:10];
    knowLabel.textColor = [Utils colorConvertFromString:@"#333333"];
    [headView addSubview:knowLabel];
    
    tagLabel = [[UILabel alloc] init];
    tagLabel.text = @"暂无数据";
    tagLabel.font = [UIFont systemFontOfSize:10];
    tagLabel.textColor = [Utils colorConvertFromString:@"#333333"];
    [headView addSubview:tagLabel];
    
    tagLabel2 = [[UILabel alloc] init];
    tagLabel2.text = @"";
    tagLabel2.font = [UIFont systemFontOfSize:10];
    tagLabel2.textColor = [Utils colorConvertFromString:@"#333333"];
    [headView addSubview:tagLabel2];
    
    tagLabel3 = [[UILabel alloc] init];
    tagLabel3.text = @"";
    tagLabel3.font = [UIFont systemFontOfSize:10];
    tagLabel3.textColor = [Utils colorConvertFromString:@"#333333"];
    [headView addSubview:tagLabel3];
    
    starView = [[Starview alloc] initWithFrame:CGRectMake(11.5*scal, headView.frame.size.height-20, 65*scal+22.5*scal, 12)];
    [headView addSubview:starView];
    
    
    [headView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.contentView.mas_left);
        make.leading.equalTo(self.contentView.mas_leading);
        make.size.mas_equalTo(CGSizeMake(KWidth, 110));
    }];
    
    [hpView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(headView.mas_top).with.offset(15);
        make.left.equalTo(headView.mas_left).with.offset(22.5*scal);
        make.size.mas_equalTo(CGSizeMake(65*scal, 65*scal));
        
    }];
    
    [starView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(headView.mas_bottom).with.offset(-15);
        make.left.equalTo(headView.mas_left).with.offset(11*scal);
        make.size.mas_equalTo(CGSizeMake(65*scal+22*scal, 10));
    }];
    
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(headView.mas_top).with.offset(15);
        make.left.equalTo(hpView.mas_right).with.offset(22.5*scal);
        make.right.equalTo(numLabel.mas_left).with.offset(-15*scal);
        make.height.equalTo(@20);
        
    }];
    
    [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(headView.mas_top).with.offset(15);
        make.left.equalTo(nameLabel.mas_right).with.offset(15*scal);
        make.right.equalTo(headView.mas_right).with.offset(-20*scal);
        make.height.equalTo(@20);
    }];
    
    [evaluateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(nameLabel.mas_bottom).with.offset(10);
        make.left.equalTo(hpView.mas_right).with.offset(22.5*scal);
        make.height.equalTo(@10);
        make.right.equalTo(headView.mas_right);
    }];
    
    
    [interImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(evaluateLabel.mas_bottom).with.offset(10);
        make.left.equalTo(hpView.mas_right).with.offset(22.5*scal);
        make.right.equalTo(belongLabel.mas_left).with.offset(-3);
        make.height.equalTo(@10);
        
    }];
    
    [belongLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(evaluateLabel.mas_bottom).with.offset(10);
        make.left.equalTo(interImgView.mas_right).with.offset(3);
        make.right.equalTo(beTextLabel.mas_left).with.offset(-3);
        make.height.equalTo(@10);
        
    }];
    
    
    [beTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(evaluateLabel.mas_bottom).with.offset(10);
        make.left.equalTo(belongLabel.mas_right).with.offset(3);
        make.right.equalTo(headView.mas_right).with.offset(-3);
        make.height.equalTo(@10);
        
    }];
    
    [estateView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(interImgView.mas_bottom).with.offset(10);
        make.left.equalTo(hpView.mas_right).with.offset(22.5*scal);
        make.right.equalTo(knowLabel.mas_left).with.offset(-3);
        make.height.equalTo(@10);
        
    }];
    
    [knowLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(interImgView.mas_bottom).with.offset(10);
        make.left.equalTo(estateView.mas_right).with.offset(3);
        make.right.equalTo(tagLabel.mas_left).with.offset(-3);
        make.height.equalTo(@10);
        
    }];
    
    [tagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(interImgView.mas_bottom).with.offset(10);
        make.left.equalTo(knowLabel.mas_right).with.offset(3);
        make.right.equalTo(tagLabel2.mas_left).with.offset(-3);
        make.height.equalTo(@10);
    }];
    
    [tagLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(interImgView.mas_bottom).with.offset(10);
        make.left.equalTo(tagLabel.mas_right).with.offset(3);
        make.right.equalTo(tagLabel3.mas_left).with.offset(-3);
        make.height.equalTo(@10);
    }];
    
    [tagLabel3 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(interImgView.mas_bottom).with.offset(10);
        make.left.equalTo(tagLabel2.mas_right).with.offset(3);
        make.right.equalTo(headView.mas_right).with.offset(-3);
        make.height.equalTo(@10);
    }];
}

- (void)setModel:(AgentModel *)model {
    
    _model = model;
    nameLabel.text = _model.name;
    [hpView sd_setImageWithURL:[NSURL URLWithString:_model.image] placeholderImage:[UIImage imageNamed:@"头像"]];
    numLabel.text = [NSString stringWithFormat:@"在租房源%@套",_model.amount];
    [self setRichNumberWithLabel:numLabel Color:[UIColor redColor] FontSize:13];
    beTextLabel.text = _model.company;
    
    NSString *professional = [_model.point_professional substringToIndex:1];
    NSString *speed = [_model.point_speed substringToIndex:1];
    NSString *service = [_model.point_service substringToIndex:1];

    evaluateLabel.text = [NSString stringWithFormat:@"专业能力%@分,响应速度%@分,服务态度%@分",professional,speed,service];
    [self setRichNumberWithLabel:evaluateLabel Color:[UIColor redColor] FontSize:13];
    
    starView.index = 4;
    
}

- (void)setRichNumberWithLabel:(UILabel*)label Color:(UIColor *)color FontSize:(CGFloat)size {
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text];
    NSString *temp = nil;
    for(int i =0; i < [attributedString length]; i++) {
        temp = [label.text substringWithRange:NSMakeRange(i, 1)];
        if ([self isPureInt:temp]) {
            [attributedString setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                             color, NSForegroundColorAttributeName,
                                             [UIFont systemFontOfSize:size],NSFontAttributeName, nil]
                                      range:NSMakeRange(i, 1)];
        }
    }
    label.attributedText = attributedString;
}

- (BOOL)isPureInt:(NSString *)string {
    NSScanner *scan = [NSScanner scannerWithString:string];
    int value;
    return [scan scanInt:&value] && [scan isAtEnd];
}


//设置不同字体颜色
-(void)setTextColor:(UILabel *)label FontNumber:(id)font AndRange:(NSRange)range AndColor:(UIColor *)vaColor
{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:label.text];
    //设置字号
    [str addAttribute:NSFontAttributeName value:font range:range];
    //设置文字颜色
    [str addAttribute:NSForegroundColorAttributeName value:vaColor range:range];
    
    label.attributedText = str;
}
@end
